# -*- coding: utf-8 -*-
import logging
from formencode import htmlfill
from formencode import validators
import formencode
import pythonblog.CaptchasDotNet as CaptchasDotNet
from pythonblog.service import user_service

log = logging.getLogger(__name__)

def custom_formatter(error):
    return '<label class="error">%s</label><br />' % htmlfill.html_quote(error)

captchas = CaptchasDotNet.CaptchasDotNet (
  client   = 'pythonblog',
  secret   = '5zCYdj8SqvztXzpWj3vLhIentlnHuI8vLwob0GaZ',
  alphabet = '123456789',
  letters  = 4,
  width    = 180,
  height   = 80
)

preservedNames = ('www', 'career', 'graduation', 'newcareer', 'senior', 'fun',
                  'webmaster', 'blog', 'app', 'news', 'bbs', 'forum', 'wiki', 'group', 'job')

class RandomValidator(formencode.FancyValidator):
    def _to_python(self, value, state):
        return value.strip()

    def validate_python(self, value, state):
        if not captchas.validate(value):
             raise formencode.Invalid(u'验证码已被使用，不能多次使用', value, state)

class CaptchasValidator(formencode.FancyValidator):
    def _to_python(self, value, state):
        return value.strip()

    def validate_python(self, value, state):
        if not captchas.verify(value):
            raise formencode.Invalid(u'您输入的验证码不对', value, state)

class EmailDupcliateValidator(validators.Email):
    def validate_python(self, value, state):
        validators.Email.validate_python(self, value, state)
        userName = None
        if state and state.userName:
            userName = state.userName
        if user_service.is_email_repeat(value, userName):
            raise formencode.Invalid(u'Email已被使用！', value, state)
        

class UserProfileForm(formencode.Schema):
    allow_extra_fields = True
    filter_extra_fields = True
    userName = formencode.All(validators.String(strip=True, not_empty=True, min=2, max=15),
                   validators.PlainText(strip=True))
    password = validators.String(not_empty=True, min=3, max=15)
    password_confirm = validators.String(not_empty=True)
    chained_validators = [validators.FieldsMatch('password', 'password_confirm', messages={'invalidNoMatch':u'两次输入的密码不一样'})]

    realName = validators.String(strip=True, min=0, max=30)
    gender = validators.Int(strip=True, min=0, max=2)
    email = formencode.All(validators.String(strip=True, min=0, max=50), EmailDupcliateValidator(strip=True))
    city = validators.String(strip=True, min=0, max=30)
    vocation = validators.String(strip=True, min=0, max=30)
    industry = validators.String(strip=True, min=0, max=30)

    random = RandomValidator(not_empty=True)
    captcha = CaptchasValidator(not_empty=True)

class UserProfileUpdateForm(formencode.Schema):
    allow_extra_fields = True
    filter_extra_fields = True
    realName = validators.String(strip=True, min=0, max=30)
    gender = validators.Int(strip=True, min=0, max=2)
    email = formencode.All(validators.String(strip=True, min=0, max=50), EmailDupcliateValidator(strip=True))
    city = validators.String(strip=True, min=0, max=30)
    vocation = validators.String(strip=True, min=0, max=30)
    industry = validators.String(strip=True, min=0, max=30)

    password = validators.String(not_empty=False, min=0, max=15)
    password_confirm = validators.String(not_empty=False, min=0, max=15)
    chained_validators = [validators.FieldsMatch('password', 'password_confirm', messages={'invalidNoMatch':u'两次输入的密码不一样'})]

